<?php
/**
 * @name Bootstrap
 * @author root
 * @desc 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用,
 * @see http://www.php.net/manual/en/class.yaf-bootstrap-abstract.php
 * 这些方法, 都接受一个参数:\Yaf\Dispatcher $dispatcher
 * 调用的次序, 和申明的次序相同
 */
class Bootstrap extends \Yaf\Bootstrap_Abstract {

    public function _initConfig() {
		//把配置保存起来
		$arrConfig = \Yaf\Application::app()->getConfig();
		\Yaf\Registry::set('config', $arrConfig);
	}

	public function _initPlugin(\Yaf\Dispatcher $dispatcher) {
		//注册一个插件
		$objSamplePlugin = new SamplePlugin();
		$dispatcher->registerPlugin($objSamplePlugin);
	}

	public function _initRoute(\Yaf\Dispatcher $dispatcher) {
		//在这里注册自己的路由协议,默认使用简单路由
		$router = Yaf\Dispatcher::getInstance()->getRouter();
		$router->addConfig(Yaf\Registry::get("config")->routes);
	}
	
	public function _initView(\Yaf\Dispatcher $dispatcher) {
		//在这里注册自己的view控制器，例如smarty,firekylin
	}

	//composer
	public function _initLoad(){
		\Yaf\Loader::import(APP_PATH . '/vendor/autoload.php');
	}

	//db
	public function _initDatabase(){
		$arrConfig = \Yaf\Registry::get('config');
		$option    = [
			'database_type' => $arrConfig->db->database_type,
			'database_name' => $arrConfig->db->database_name,
			'server'        => $arrConfig->db->server,
			'username'      => $arrConfig->db->username,
			'password'      => $arrConfig->db->password,
			'prefix'        => $arrConfig->db->prefix ?? '',
			'charset'       => $arrConfig->db->charset ?? 'utf8mb4',
			'logging'       => $arrConfig->db->logging,
			'option'        => [
				PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC,
				PDO::ATTR_STRINGIFY_FETCHES=>false,
				PDO::ATTR_EMULATE_PREPARES=>false,
			],
		];
		\Yaf\Registry::set('db', new \Medoo\Medoo($option));
	}
}
